#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <string>

// ====== 模拟 class_loader_utility.h 里的接口 ======
namespace apollo {
namespace cyber {
    namespace class_loader {
        namespace utility {

            // 工厂表：key = "Derived"，value = 创建 Base* 的函数
            static std::map<std::string, std::function<void*()>> g_factory;

            // Apollo 的 RegisterClass 思路：Base 必须是 Derived 的基类
            template <typename Derived, typename Base>
            void RegisterClass(const std::string& d, const std::string& b)
            {
                // 编译期检查：Derived 必须能隐式转换为 Base*
                static_assert(std::is_base_of<Base, Derived>::value,
                    "Derived must inherit from Base");

                g_factory[d] = []() -> void* {
                    return static_cast<Base*>(new Derived());
                };

                std::cout << "Register class: " << d << " as subclass of " << b << std::endl;
            }

            // 提供一个创建函数（产出 Base*）
            template <typename Base>
            std::unique_ptr<Base> Create(const std::string& d)
            {
                auto it = g_factory.find(d);
                if (it != g_factory.end()) {
                    return std::unique_ptr<Base>(static_cast<Base*>((it->second)()));
                }
                return nullptr;
            }

        } // namespace utility
    } // namespace class_loader
} // namespace cyber
} // namespace apollo

// ====== 你给的宏定义 ======
#define CLASS_LOADER_REGISTER_CLASS_INTERNAL(Derived, Base, UniqueID)               \
    namespace {                                                                     \
        struct ProxyType##UniqueID {                                                \
            ProxyType##UniqueID()                                                   \
            {                                                                       \
                apollo::cyber::class_loader::utility::RegisterClass<Derived, Base>( \
                    #Derived, #Base);                                               \
            }                                                                       \
        };                                                                          \
        static ProxyType##UniqueID g_register_class_##UniqueID;                     \
    }

#define CLASS_LOADER_REGISTER_CLASS_INTERNAL_1(Derived, Base, UniqueID) \
    CLASS_LOADER_REGISTER_CLASS_INTERNAL(Derived, Base, UniqueID)

#define CLASS_LOADER_REGISTER_CLASS(Derived, Base) \
    CLASS_LOADER_REGISTER_CLASS_INTERNAL_1(Derived, Base, __COUNTER__)

// ====== 应用部分 ======
struct Driver {
    virtual ~Driver() = default;
    virtual void Run() = 0;
};

struct Derived1 : public Driver {
    void Run() override { std::cout << "Derived1 running\n"; }
};

struct Derived2 : public Driver {
    void Run() override { std::cout << "Derived2 running\n"; }
};

// 使用宏注册类
CLASS_LOADER_REGISTER_CLASS(Derived1, Driver)
CLASS_LOADER_REGISTER_CLASS(Derived2, Driver)

int main()
{
    using namespace apollo::cyber::class_loader::utility;

    // 创建对象
    auto d1 = Create<Driver>("Derived1");
    auto d2 = Create<Driver>("Derived2");

    d1->Run(); // 输出 Derived1 running
    d2->Run(); // 输出 Derived2 running

    return 0;
}
